<?php

/**
 * This is the model class for table "competency_element".
 *
 * The followings are the available columns in table 'competency_element':
 * @property string $COMP_ELEM_CODE
 * @property string $COMP_UNIT_CODE
 * @property string $COMP_ELEM_NAME
 * @property string $COMP_ELEM_DESC
 * @property integer $USER_CREATED
 * @property string $TIME_CREATED
 * @property integer $USER_UPDATED
 * @property string $TIME_UPDATED
 *
 * The followings are the available model relations:
 * @property CompetencyUnit $cOMPUNITCODE
 * @property CompetencyLibrary[] $competencyLibraries
 */
class CompetencyElement extends CMasterModel
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return CompetencyElement the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'competency_element';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('USER_CREATED, USER_UPDATED', 'numerical', 'integerOnly'=>true),
			array('COMP_ELEM_CODE, COMP_UNIT_CODE', 'length', 'max'=>10),
			array('COMP_ELEM_NAME', 'length', 'max'=>200),
			array('COMP_ELEM_DESC', 'length', 'max'=>1000),
			array('TIME_CREATED, TIME_UPDATED', 'safe'),
			array('COMP_ELEM_DESC,COMP_ELEM_CODE,COMP_ELEM_NAME,COMP_UNIT_CODE', 'required'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('COMP_ELEM_CODE, COMP_UNIT_CODE, COMP_ELEM_NAME, COMP_ELEM_DESC, USER_CREATED, TIME_CREATED, USER_UPDATED, TIME_UPDATED', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cOMPUNITCODE' => array(self::BELONGS_TO, 'CompetencyUnit', 'COMP_UNIT_CODE'),
			'competencyLibraries' => array(self::HAS_MANY, 'CompetencyLibrary', 'COMP_ELEM_CODE'),
		);
	}
	
	public function getElementCodeAndUnit() {
      return $this->COMP_UNIT_CODE . ' (' . $this->COMP_ELEM_CODE . ')'; 
    }
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'COMP_ELEM_CODE' => 'Comp Elem Code',
			'COMP_UNIT_CODE' => 'Comp Unit Code',
			'COMP_ELEM_NAME' => 'Comp Elem Name',
			'COMP_ELEM_DESC' => 'Comp Elem Desc',
			'USER_CREATED' => 'User Created',
			'TIME_CREATED' => 'Time Created',
			'USER_UPDATED' => 'User Updated',
			'TIME_UPDATED' => 'Time Updated',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('COMP_ELEM_CODE',$this->COMP_ELEM_CODE,true);
		$criteria->compare('COMP_UNIT_CODE',$this->COMP_UNIT_CODE,true);
		$criteria->compare('COMP_ELEM_NAME',$this->COMP_ELEM_NAME,true);
		$criteria->compare('COMP_ELEM_DESC',$this->COMP_ELEM_DESC,true);
		/*$criteria->compare('USER_CREATED',$this->USER_CREATED);
		$criteria->compare('TIME_CREATED',$this->TIME_CREATED,true);
		$criteria->compare('USER_UPDATED',$this->USER_UPDATED);
		$criteria->compare('TIME_UPDATED',$this->TIME_UPDATED,true);
		*/
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function getUnitId()
	{
		$sql = "select COMP_UNIT_CODE	
			from competency_unit
			order by COMP_UNIT_CODE";
		
		$rows = Yii::app()->db->createCommand($sql)->queryAll();
			
			
		$arrUnitCode = array();		
		
		foreach($rows as $key=>$value)
		{
			$uCode = $value['COMP_UNIT_CODE'];
			
			$arrUnitCode["$uCode"] = $uCode;
		}
		
		//print_r($arrUnitCode);
			
		return $arrUnitCode;//array('1'=>'10','2'=>'20')
	
		//$this->renderRows($rows);

	}
}